package xcmg.device.web.borrowMaterial;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.borrowMaterial.BorrowMaterialDO;
import xcmg.device.dao.entity.borrowMaterial.BorrowMaterialMesReq;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ConstantInfo;
import xcmg.device.service.borrowMaterial.BorrowMaterialService;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ZHANGK
 * @since  2020/6/11
 **/
@RestController
@RequestMapping("/borrowMaterial")
public class BorrowMaterialController extends BasicController {

    @Autowired
    BorrowMaterialService borrowMaterialService;

    @PostMapping("/add")
    public ActionResult add(@RequestBody BorrowMaterialDO borrowMaterialDO) throws Exception {
        String orgId = getOrgId();
        borrowMaterialDO.setOrgId(orgId);
        return actionResult(borrowMaterialService.insert(borrowMaterialDO));
    }

    @PostMapping("/delete")
    public ActionResult delete(@RequestBody BorrowMaterialDO borrowMaterialDO) throws Exception {
        String orgId = getOrgId();
        return actionResult(borrowMaterialService.deleteById(borrowMaterialDO,orgId));
    }

    @PostMapping("/update")
    public ActionResult update(@RequestBody BorrowMaterialDO borrowMaterialDO) throws Exception {
        borrowMaterialDO.setOrgId(getOrgId());
        return actionResult(borrowMaterialService.updateById(borrowMaterialDO));
    }

    @PostMapping("/list")
    public ActionResult<PageInfo> list(@RequestBody SearchCommonVO<BorrowMaterialDO> condition){
        PageCommonVO<BorrowMaterialDO> pageCommonVO = borrowMaterialService.selectList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping("/get")
    public ActionResult<BorrowMaterialDO> get(@RequestParam("id") String id){
        return actionValueResult(borrowMaterialService.selectById(id));
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public ActionResult audit(@RequestBody BorrowMaterialDO borrowMaterialDO) throws Exception {
        borrowMaterialDO.setOneLevelAuditStatus(ConstantInfo.BORROW_ONE_LEVEL_STATUS_SUCCESS);
        return actionResult(borrowMaterialService.audit(borrowMaterialDO));
    }

    @PostMapping("/refuse")
    public ActionResult refuse(@RequestBody BorrowMaterialDO borrowMaterialDO){
        borrowMaterialDO.setOneLevelAuditStatus(ConstantInfo.BORROW_ONE_LEVEL_STATUS_REFUSE);
        return actionResult(borrowMaterialService.updateById(borrowMaterialDO));
    }

//    @PostMapping("/pushToMes")
//    public ActionResult pushToMes(@RequestBody BorrowMaterialDO borrowMaterialDO){
//        borrowMaterialDO.setOneLevelAuditStatus(ConstantInfo.BORROW_ONE_LEVEL_STATUS_REFUSE);
//        return actionResult(borrowMaterialService.updateById(borrowMaterialDO));
//    }

    @PostMapping("/borrowAudit")
    public Map borrowAudit(@RequestBody BorrowMaterialMesReq borrowMaterialMesReq){
        BorrowMaterialDO borrowMaterialDO = new BorrowMaterialDO();
        BeanUtils.copyProperties(borrowMaterialMesReq,borrowMaterialDO);
        borrowMaterialDO.setTwoLevelAuditStatus(borrowMaterialMesReq.getAuditResult());
        borrowMaterialDO.setTwoLevelAuditDate(new Date());
        boolean result = borrowMaterialService.updateByCode(borrowMaterialDO);

        //mes要求做特殊返回
        Map resultMap = new HashMap();
        resultMap.put("success",result);
        if (result){
            resultMap.put("message","操作成功");
            resultMap.put("value","操作成功");
        }else{
            resultMap.put("message","操作失败");
            resultMap.put("value","操作失败");
        }
        return resultMap;
    }

    @GetMapping("/getPrintList")
    public ActionResult getPrintList(String[] ids){
        return actionValueResult(borrowMaterialService.selectPrintList(Arrays.asList(ids)));
    }

    @PostMapping("/batchDelete")
    public ActionResult batchDelete(@RequestBody String[] ids) throws Exception {
        String orgId = getOrgId();
        return actionResult(borrowMaterialService.batchDelete(ids,orgId));
    }

    @RequestMapping(value = "/export", method = {RequestMethod.POST, RequestMethod.GET})
    public void export(String[] ids) throws Exception {
        borrowMaterialService.export(ids);
    }

    @RequestMapping(value = "/exportAll", method = {RequestMethod.POST, RequestMethod.GET})
    public void exportAll(BorrowMaterialDO borrowMaterialDO) throws Exception {
        borrowMaterialService.exportAll(borrowMaterialDO);
    }

}
